package com.file.manage.infrastructure.config;


import cn.dev33.satoken.stp.StpUtil;
import com.file.manage.domain.data.UserInfo;
import com.file.manage.domain.enums.UserRole;

/**
 * @author H41
 * @Type CurrentUserCache.java
 * @date 2025/5/30 23:42
 * @Desc 缓存当前登录用户信息
 */
public class CurrentUserCache {

    public static UserInfo get() {
        Object loginId = StpUtil.getLoginId();
        return loginId == null ? null : (UserInfo) StpUtil.getSession().get(loginId.toString());
    }

    public static String getId() {
        Object loginId = StpUtil.getLoginId();
        UserInfo userInfo = loginId == null ? null : (UserInfo) StpUtil.getSession().get(loginId.toString());
        return userInfo != null ? userInfo.getId() : null;
    }

    public static boolean isAdmin() {
        Object loginId = StpUtil.getLoginId();
        UserInfo userInfo = loginId == null ? null : (UserInfo) StpUtil.getSession().get(loginId.toString());
        return userInfo != null && userInfo.getRole() == UserRole.ADMIN;
    }
}
